package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description TODO
 * @Author itheima
 * @Date 2023-07-19 17:54
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 添加商品到购物车相关接口
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //创建购物车的实体类
        ShoppingCart shoppingCart = new ShoppingCart();
        //将DTO实体类的数据复制给购物车
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //拿到用户的id
        Long currentId = BaseContext.getCurrentId();
        //再创建一个实体类abshoppingCart用来接收数据
        ShoppingCart abshoppingCart= null;
        //将用户的id给shoppingCart实体类
        shoppingCart.setUserId(currentId);
        //因为前端每一次只传一种数据 所以判断传过来的是套餐或者菜品
        if (shoppingCartDTO.getDishId()!=null){
            //为菜品的数据 根据菜品 id合口味 数据查询购物车表
            abshoppingCart= shoppingCartMapper.gedishid(shoppingCart);
        }else {
            //为套餐表  根据套餐id来查询购物车
            abshoppingCart= shoppingCartMapper.getsettmealid(shoppingCart);

        }
        //判断查询出来的实体类是否为null
        if (abshoppingCart==null) {
            //为null则购物车没有数据则补全实体类然后插入购物车表
            abshoppingCart=shoppingCart;

            //因为前端每一次只传一种数据 所以判断传过来的是套餐或者菜品
            if (shoppingCartDTO.getDishId()!=null){
                //1.1.1不为nuul 根据dishid查询菜品表的数据用购物车的实体类接收
                Dish dishVO = dishMapper.getbydishid(shoppingCartDTO.getDishId());
                abshoppingCart.setName(dishVO.getName());
                abshoppingCart.setImage(dishVO.getImage());
                abshoppingCart.setAmount(dishVO.getPrice());
            }else {

                //1.1.2为null  则查询套餐表的数据用购物车的实体类接收

                Setmeal setmealVO = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                abshoppingCart.setName(setmealVO.getName());
                abshoppingCart.setImage(setmealVO.getImage());
                abshoppingCart.setAmount(setmealVO.getPrice());

            }
            //补全时间
            abshoppingCart.setCreateTime(LocalDateTime.now());
            //默认数量
            abshoppingCart.setNumber(1);
            //插入购物车表
            shoppingCartMapper.add(abshoppingCart);
        }else {
            //2.如果购物车菜品或者套餐存在则数量+1
            abshoppingCart.setNumber(abshoppingCart.getNumber() + 1);
            //修改数据
            shoppingCartMapper.updateNumber(abshoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {//因为购物车 是用户操作的所以查找数据是用的用户id
        Long userid = BaseContext.getCurrentId();
        List<ShoppingCart> sholpingCartlist=shoppingCartMapper.findByUserId(userid);
        return sholpingCartlist;
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        Long userid = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userid);
    }

    /**
     * 删除购物车的商品
     * @param shoppingCartDTO
     */
    @Override
    public void deletesub(ShoppingCartDTO shoppingCartDTO) {
        //创建购物车实体类
        ShoppingCart shoppingCart = new ShoppingCart();
        //将DTO拷贝给实体类
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //拿到用户id
        Long userid = BaseContext.getCurrentId();
        //给实体类
        shoppingCart.setUserId(userid);
        //定义一个实体类变量
        ShoppingCart shoppingCart1 =null;
        //判断是菜品请求还是套餐请求
        if (shoppingCartDTO.getDishId()!=null){
            //是菜品查出购物车里菜品的数据
            shoppingCart1= shoppingCartMapper.gedishid(shoppingCart);
        }else {
            //是套餐查出购物车套餐里的数据
            shoppingCart1= shoppingCartMapper.getsettmealid(shoppingCart);
        }
        //购物车里面有数据则数量减一
        if (shoppingCart1!=null){
            //菜品数量或套餐数量
            Integer number = shoppingCart1.getNumber()-1;
            //当减一后数量为0时
            if (number==0){
                //删除
                shoppingCartMapper.delete(shoppingCart1.getId());
            }
            //修改数量
            shoppingCart1.setNumber(number);
            shoppingCartMapper.updateNumber(shoppingCart1);

        }
    }
}
